﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Edge.Business.Domain.Application;
using Edge.Common.Services;
using Shop.Business.Domain.Catalog;
using Shop.Business.Domain.Customers;
using Shop.Web.AppCode;

namespace Shop.Web
{
    public partial class BrandList : ShopBasePage
    {
        private bool delVisible = false;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            HandlePage();
            DataBind();
        }

        private void DataBind()
        {
            grid.DataSource = Locator.GetService<IBrandManager>().FindAll();
            grid.DataBind();
        }


        protected void grid_DataBinding(object sender, GridViewRowEventArgs e)
        {
            Button btn = (Button) e.Row.FindControl("btnDelete");
            if (btn != null)
                btn.Visible = delVisible;
            Brand brand = (Brand)e.Row.DataItem;
            if (brand != null)
            {
                Image imgLogo = (Image)e.Row.FindControl("imgLogo");
                if (imgLogo != null)
                {
                    imgLogo.ImageUrl = ShopBasePage.GetImagePath(brand);
                }
            }
        }

        protected void HandlePage()
        {
            if (Locator.GetService<IContextObjects>().Get<CustomerHolder>() == null)
                AccessDeniedMessage();
            else
            {
                Customer cc = Locator.GetService<IContextObjects>().Get<CustomerHolder>().Object;
                switch (cc.Role)
                {
                    case CustomerRole.Buyer:
                        AccessDeniedMessage();
                        break;
                    case CustomerRole.Manager:
                        break;
                    case CustomerRole.Admin:
                        delVisible = true;
                        break;
                    default:
                        break;

                }
            }
    }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            Brand brand = null;
            int id = Convert.ToInt32(((Button)sender).CommandArgument);
            if (id > 0)
                brand = Locator.GetService<IBrandManager>().Load(id);
            if (brand != null)
            {
                Locator.GetService<IBrandManager>().Delete(brand);
            }
            DataBind();
        }
    }
}
